home *** CD-ROM | disk | FTP | other *** search
- Path: library.erc.clarkson.edu!rpi!not-for-mail
- From: Alexander Stepanov <stepanov@murrow.corp.sgi.com>
- Newsgroups: comp.lang.c++.moderated,comp.lang.c++,comp.os.ms-windows.programmer.misc
- Subject: Re: STL loops through a list with one element more than once! Why?
- Date: 3 Jan 1996 09:26:38 -0000
- Organization: Silicon Graphics, Inc.
- Sender: cppmods@netlab.cs.rpi.edu
- Approved: kanze@gabi-soft.fr
- Message-ID: <4cdi4e$cer@netlab.cs.rpi.edu>
- References: <4c1n8o$2jn@netlab.cs.rpi.edu> <4c96ot$1ve@netlab.cs.rpi.edu>
- NNTP-Posting-Host: netlab.cs.rpi.edu
-
- X-Original-Date: 2 Jan 1996 19:18:24 GMT
-
- terris@rahul.net (Terris Linenbach) wrote:
- >X-Original-Date: 1 Jan 1996 05:46:03 GMT
- >
- >"erase" invalidates all active iterators on the container.
-
- I think it is not quite true. Erase invalidates some active iterators on the
- container. In case of list, erase invalidates only those iterators that point
- to the erased elements.
-
- In this particular case:
-
- for (_itemIter = _itemList.begin();
- _itemIter != _itemList.end();
- _itemIter++) // <- itemIter is invalid now
- {
- _itemList.erase (_itemIter);
- }
-
- But there is a simple fix:
-
- for (_itemIter = _itemList.begin();
- _itemIter != _itemList.end(); )
- {
- _itemList.erase (_itemIter++);
- }
-
- and, of course,
-
- _itemList.erase(_itemList.begin(), _itemList.end());
-
- will do the same thing.
-
-
-
- Alex Stepanov
- stepanov@mti.sgi.com
-
- Silicon Graphics
- 2011 N. Shorline Boulevard
- Mountain View, CA 94043-1389
-
- tel.: (415)933-6121
-
-
- [ comp.lang.c++.moderated is a moderated newsgroup. Submit articles ]
- [ to <c++-submit@netlab.cs.rpi.edu>. The moderation policy can be ]
- [ retrieved from <http://netlab.cs.rpi.edu/~cppmods/guide.html>. ]
- [ Moderators can be reached at: c++-request@netlab.cs.rpi.edu. ]
-